package com.tuoren.forward.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson2.JSONObject;

@Component
public class RemoteUtil {
	
	@Value("${remote.hospitalCodeAddress}")
	private String hospitalCodeAddress;
	@Autowired
	RestTemplate restTemplate;
	
	public String localtion(String ci,String lac) {
		HttpHeaders headers = new HttpHeaders();
	    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
	    
	    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	    map.add("mcc", "460");
	    map.add("mnc", "0");
	    map.add("lac",lac);
	    map.add("ci",ci);
	    map.add("hex","10");
	    
	    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
	    
		String result = restTemplate.postForObject("http://bs.openluat.com/get_gps", request, String.class);
		System.out.println("result========="+result);
		JSONObject json = JSONObject.parseObject(result);
		if(json.containsKey("result")) {
			return json.getString("result");
		}
		return "";
	}
	
	public String getHospitalCode(String mac) {

	    HttpEntity<String> request = new HttpEntity<String>(mac);
		String result = restTemplate.postForObject(hospitalCodeAddress, request, String.class);
		System.out.println("result========="+result);
		JSONObject json = JSONObject.parseObject(result);
		if(json.containsKey("data")) {
			return json.getString("data");
		}
		return "";
	}

}
